package droid.frame.activity.base;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;

import java.util.ArrayList;
import java.util.List;

/**
 * @param <T>2013下午4:11:51
 * @author coffee<br>
 */
public abstract class BaseAdapterExt<T> extends android.widget.BaseAdapter {

    protected Activity context;

    protected ArrayList<T> items;

    private long itemId;
    /**
     * items中的元素的个数的限制，0代表没有限制
     */
    protected int MAX_SIZE = 0;
    /**
     * 列表为空的时候显示的view , 可以是int resource 或者 view
     */
    private Object emptyView;

    /**
     * 传入listview或者子类,或者用于动态addView的LineraLayout等
     */
    protected ViewGroup listView;

    /********************* 以下是自定义 ************************/
    // protected BaseAdapter(int size) {
    // this.MAX_SIZE = size;
    // }
    public BaseAdapterExt(ArrayList<T> items, Activity context) {
        this(items, context, null);
    }

    /**
     * {@link #filledInLayout(ViewGroup)}
     *
     * @param items
     * @param context
     * @param listView
     */
    public BaseAdapterExt(ArrayList<T> items, Activity context, ViewGroup listView) {
        this.items = items;
        this.context = context;
        this.listView = listView;
        if (this.items == null) {
            this.items = new ArrayList<T>();
        }
    }

    /********************* 以下是自定义 ************************/

    public void setMaxSize(int maxSize) {
        this.MAX_SIZE = maxSize;
    }

    /**
     * 设置完数据以后是否直接刷新
     *
     * @param items    参数为 null, 则清空adater的数据
     * @param isNotify
     */
    public void setItems(ArrayList<T> items, boolean isNotify) {
        if (items == null) {
            items = new ArrayList<T>();
        }
        this.items = items;
        if (this.MAX_SIZE != 0) {
            // 只取前MAX_SIZE个元素
            if (this.items.size() > MAX_SIZE) {
                this.items = (ArrayList<T>) this.items.subList(0, MAX_SIZE);
            }
        }
        if (isNotify) {
            notifyDataSetChanged();
        }
    }

    /**
     * {@link #isEnabled(int)}
     *
     * @return
     */
    protected boolean hasEmptyView() {
        return emptyView != null && items.size() == 0;
    }

    /**
     * 当listview 数据为空的时候显示
     *
     * @param emptyResOrView , res 或者view
     */
    public void setEmptyView(Object emptyResOrView) {
        this.emptyView = emptyResOrView;
    }

    /**
     * 新增一个元素<br>
     *
     * @param obj
     * @param isNotify ： 是否需要刷新
     */
    public void appendItem(T obj, boolean isNotify) {
        this.items.add(obj);
        if (this.MAX_SIZE != 0) {
            // 只取最后MAX_SIZE个元素
            if (this.items.size() > MAX_SIZE) {
                this.items = (ArrayList<T>) this.items.subList(this.items.size() - MAX_SIZE, this.items.size());
            }
        }
        if (isNotify) {
            notifyDataSetChanged();
        }
    }

    public void appendItems(List<T> items, boolean isNotify) {
        if (items == null) {
            items = new ArrayList<T>();
        }
        this.items.addAll(items);
        if (isNotify) {
            notifyDataSetChanged();
        }
    }

    public ArrayList<T> getItems() {
        return this.items;
    }

    public String getPrice(String priceText) {
        return "￥" + priceText;
    }

    protected ViewGroup getListView() {
        return listView;
    }

    public void filledInLayout(ViewGroup viewgroup) {
        viewgroup.removeAllViews();
        for (int i = 0; i < getCount(); i++) {
            viewgroup.addView(getView(i, null, null));
        }
    }

    /********************* 以下是父类实现 ************************/
    /**
     * 注意该方法被重写了, {@link #isEmpty()}也必须被重写<br>
     * 如果需要返回items数量,请调用{@link #getItemsSize()}、{@link #isEmpty()}
     */
    @Override
    @Deprecated
    public int getCount() {
        if (hasEmptyView()) {
            return 1;
        } else {
            return items.size();
        }
    }

    /**
     *
     */
    @Override
    public boolean isEmpty() {
        return items.size() == 0;
    }

    public int getItemsSize() {
        return items == null ? 0 : items.size();
    }

    @Override
    public T getItem(int position) {
        if (items != null && items.size() > 0) {
            return items.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return itemId;
    }

    public void setItemId(long itemId) {
        this.itemId = itemId;
    }

    /**
     * 如果抛异常会导致程序崩溃, 子类需要捕获并处理<br>
     *
     * @param itemId
     * @throws ClassCastException
     */
    public void setItemId(String itemId) throws ClassCastException {
        this.itemId = Long.valueOf(itemId);
    }

    /**
     * 单击item会先调用该方法, 如果返回false表示该item不会触发onItemClickListener事件
     */
    @Override
    public boolean isEnabled(int position) {
        if (hasEmptyView()) {
            return false;// 第一个item不可点击
        }
        return super.isEnabled(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (emptyView instanceof View) {
            return (View) emptyView;
        } else {
            try {
                Integer res = Integer.valueOf(emptyView + "");
                View view = LayoutInflater.from(context).inflate(res, parent, false);
                // 设置view的高度 (充满屏幕) parent 就是对应的ListView
                AbsListView.LayoutParams params = (LayoutParams) view.getLayoutParams();
                params.height = parent.getHeight();
                params.width = parent.getWidth();
                view.setLayoutParams(params);
                return view;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    public int getColor(int colorRes) {
        return context.getResources().getColor(colorRes);
    }

}
